<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="50%">
    <div>
      <iframe
        ref="iframe"
        :src="alipayUrl"
        frameborder="no"
        style="border: none; width: 100%; height: 80%"
      ></iframe>
    </div>
    <!--         :width="iframeWidth"
        :height="iframeHeight" -->
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as TenantApi from '@/api/system/tenant'
import * as OrderApi from '@/api/mall/trade/order'
import * as payOrderApi from '@/api/pay/order'
defineOptions({ name: 'SystemTenantForm' })

const { t } = useI18n() // 国际化
const alipayUrl = ref() //内容
const iframeWidth = ref('100%') //内嵌网页宽度
const iframeHeight = ref('100%') //内嵌网页宽度

const message = useMessage() // 消息弹窗
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const payForm = ref<OrderApi.OrderVO>()
const submitOrderForm = ref<payOrderApi.OrderVO>()
const iframe = ref('')
// 内嵌网页宽高设置
const iframeHandleResize = () => {
  const iframeDocument = iframe.value.contentDocument || iframe.value.contentWindow.document
  let iframelet = iframeDocument
  debugger
  iframeWidth.value = iframeDocument.documentElement.scrollWidth + 'px'
  iframeHeight.value = '1024px'
  console.log(
    'iframeDocument.documentElement.scrollWidth ' +
      iframeDocument.documentElement.scrollWidth +
      ' iframeHeight ' +
      iframeHeight.value
  )
}

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  // UNPAID(0, "待支付"),
  //  CANCELED(40, "已取消");
  OrderApi.getOrderPage()

  dialogVisible.value = true
  dialogTitle.value = '扫码支付'
  payForm.value = {
    deliveryType: 2,
    pointStatus: false,
    items: [{ skuId: id, count: 1 }]
  }
  debugger
  let tradeData = await OrderApi.createOrder(payForm.value)
  // let tradeData = { payOrderId: 13 }
  debugger
  submitOrderForm.value = { id: tradeData.payOrderId, channelCode: 'alipay_pc' }
  let payData = await payOrderApi.submitOrder(submitOrderForm.value)
  debugger
  // 修改时，设置数据
  if (payData.status == 0) {
    alipayUrl.value = payData.displayContent
  }
  formLoading.value = false

  // 加载套餐列表
}
defineExpose({ open, iframeHandleResize }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 提交请求
  formLoading.value = true
  try {
    if (formType.value === 'create') {
      message.success(t('common.createSuccess'))
    } else {
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>
